package com.erhuogo.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.erhuogo.bean.GridPageBean;
import com.erhuogo.dao.SchoolDao;
import com.erhuogo.entity.School;
import com.erhuogo.enumerate.OpenStatus;
import com.erhuogo.service.SchoolService;
import com.erhuogo.util.DateUtils;

/**
 * 高校Service的实现类
 * @author aibinxiao
 * @date 2016年10月26日 下午12:03:58
 */
public class SchoolServiceImpl implements SchoolService {
	// 数据json格式转换
	private static ObjectMapper mapper = new ObjectMapper();
	
	// 日志
	private static Logger logger = LoggerFactory.getLogger(SchoolServiceImpl.class);
		
	// 注入SchoolDao
	private SchoolDao schoolDao;

	public SchoolDao getSchoolDao() {
		return schoolDao;
	}

	public void setSchoolDao(SchoolDao schoolDao) {
		this.schoolDao = schoolDao;
	}

	// 1.根据城市Id获取所有的高校Service实现类
	@Override
	public List<School> findSchoolsByCityId(Integer cityId) {
		List<School> listSchools = null;
		if (cityId != null){
		    listSchools = schoolDao.findCitysByProId(cityId);
		}
		return listSchools;
	}

	/**
	 * 2.分页条件查询符合条件的高校集合Service实现类
	 */
	@Override
	public GridPageBean<School> findByPage(String pageNo, String pageSize, String id, String name, String startDate,
			String endDate, String provinceId, String cityId) {
		Integer pNo = 1;
		Integer pSize = 10;
		if(!StringUtils.isEmpty(pageNo) && !StringUtils.isEmpty(pageSize)){
			pNo = Integer.valueOf(pageNo);
			pSize = Integer.valueOf(pageSize);
		}
		int records = schoolDao.countByParams(id, name, startDate, endDate, provinceId,cityId);
		GridPageBean<School> result = new GridPageBean<School>(pNo, records, pSize);
		//查询分页后的具体记录
		if(records <= result.getPageSize()){
			List<School> rows = schoolDao.findPageByParams(id,name,startDate,endDate,provinceId,cityId,result.getStartIndex(),records);
			result.setRows(rows);
		} else{
			List<School> rows = schoolDao.findPageByParams(id,name,startDate,endDate,provinceId,cityId,result.getStartIndex(),result.getPageSize());
			result.setRows(rows);
		}
		result.setPage(pNo);
		result.setRecords(records);
		result.setTotal(result.getTotal());
		return result;
	}

	/**
	 * 3.根据高校id获取高校信息Service实现类
	 */
	@Override
	public School findById(String idStr) {
		School school = null;
		if(!StringUtils.isEmpty(idStr)){
			Integer id = Integer.valueOf(idStr);
			school = schoolDao.findSchoolById(id);
		}
		return school;
	}

	/**
	 * 4.更新或者保存高校信息Service实现类
	 */
	@Override
	public void save(String idStr, String name, String provinceIdStr, String cityIdStr) throws Exception {
		School school = new School();
		// 如果id不为空则是修改更新
		if(!StringUtils.isEmpty(idStr)){
			Integer id = Integer.valueOf(idStr);
			school = schoolDao.findSchoolById(id);
		}else{
			// id为空则是添加新的三级分类
			school.setCreateTime(DateUtils.DateToStr(new Date()));
			school.setStatus(OpenStatus.OPEN.getStatusCode());
		}
		if(!StringUtils.isEmpty(name)){
			school.setName(name);
		}
		if(!StringUtils.isEmpty(provinceIdStr)){
			Integer provinceId = Integer.valueOf(provinceIdStr);
			school.setProvinceId(provinceId);
		}
		if(!StringUtils.isEmpty(cityIdStr)){
			Integer cityId = Integer.valueOf(cityIdStr);
			school.setCityId(cityId);
		}
		school.setAlterTime(DateUtils.DateToStr(new Date()));
		schoolDao.saveSchool(school);
	}

	/**
	 * 5.根据id、状态更新高校启用状态Service实现类
	 */
	@Override
	public void updateStatus(String idStr, String statusStr) throws Exception {
		Integer status = null;
		Integer id = null;
		if(StringUtils.isEmpty(idStr)){
			throw new Exception("id参数不能为空");
		}else{
			id = Integer.valueOf(idStr);
		}
		if(StringUtils.isEmpty(statusStr)){
			throw new Exception("转换状态参数不能为空");
		}
		if(!StringUtils.isEmpty(statusStr)){
			status = Integer.valueOf(statusStr);
		}
		//开启
		if(OpenStatus.OPEN.getStatusCode()==status){
			schoolDao.updateStauts(id, OpenStatus.OPEN.getStatusCode());
		}
		//屏蔽
		if(OpenStatus.SHIELD.getStatusCode()==status){
			schoolDao.updateStauts(id, OpenStatus.SHIELD.getStatusCode());
		}
	}
	
}
